package com.sys.orders.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sys.common.exception.GoodsStockException;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.pojo.JSONEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.AGoods;
import com.sys.goods.service.IGoodsDbService;
import com.sys.orders.dao.IAOrdersDao;
import com.sys.orders.dao.IAOrdersGoodsDao;
import com.sys.orders.pojo.AOrders;
import com.sys.orders.pojo.AOrdersGoods;
import com.sys.orders.service.IOrdersDbService;
import com.sys.users.pojo.ACart;
import com.sys.users.pojo.ACartEnum;
import com.sys.users.pojo.AUsers;
import com.sys.users.service.IUsersDbService;

/**
 * Service实现类(订单代码)
 * 
 * @author 
 */
@Service("ordersDbService")
public class OrdersDbServiceImpl implements IOrdersDbService
{
	@Autowired
	private IAOrdersDao ordersDao;
	@Autowired
	private IAOrdersGoodsDao ordersGoodsDao;
	
	@Autowired
	private IUsersDbService usersDbService;
	@Autowired
	private IGoodsDbService goodsDbService;
	
	@Override
	public ApiResponse<Object> saveOneOrdersService(AOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 判断折扣 */
		/* 判断是否是会员 */
		condMap.put("id", orders.getUsersId());
		ApiResponse<AUsers> usersApiResponse = this.usersDbService.findOneUsersService(condMap);
		AUsers users = usersApiResponse.getDataOneJava() ;
		double discount = users.getDiscount() ; 
		orders.setDiscount(discount);
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		int res = this.ordersDao.save(orders);
		if (res > 0)
		{
			double totalPrice = 0 ; 
			/* 存储订单明细 */
			for (Iterator iterator = orders.getCartList().iterator(); iterator.hasNext();)
			{
				ACart cart = (ACart) iterator.next();
				AGoods goods = cart.getGoods() ; 
				
				if(goods.getStockNum() < cart.getNum())
				{
					ConstatFinalUtil.SYS_LOGGER.info("==库存不够:{}==",goods.getId());
					/* 用户名已经存在,请换一个 */
					throw new GoodsStockException(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""));
				}
				
				AOrdersGoods ordersGoods = new AOrdersGoods();
				ordersGoods.setOrdersId(orders.getId());
				ordersGoods.setGoodsId(cart.getGoodsId());
				
				ordersGoods.setName(goods.getName());
				ordersGoods.setTitle(goods.getTitle());
				ordersGoods.setPrice(goods.getPrice());
				ordersGoods.setNum(cart.getNum());
				ordersGoods.setTotalPrice(ordersGoods.getPrice() * ordersGoods.getNum());
				
				/* 总价格相加 */
				totalPrice = totalPrice + ordersGoods.getTotalPrice() ; 
				
				ordersGoods.setStatus(orders.getStatus());
				ordersGoods.setPubTime(new Date());
				/* 订单商品 */
				this.saveOneOrdersGoodsService(ordersGoods);
				
				cart.setNum(0);
				cart.setStatus(ACartEnum.STATUS_DISABLED.getStatus());
				cart.setUpdateTime(new Date());
				
				/* 清空购物车 */
				this.usersDbService.updateOneCartService(cart);
				
				/* 更新商品的销量 */
				goods.setSaleNum(goods.getSaleNum() + ordersGoods.getNum());
				goods.setStockNum(goods.getStockNum() - ordersGoods.getNum());
				goods.setUpdateTime(new Date());
				this.goodsDbService.updateOneGoodsService(goods);
			}
			
			/* 设置总价格 */
			orders.setTotalPrice(totalPrice);
			/* 应付金额,打折 */
			orders.setDealAmount(orders.getTotalPrice() * orders.getDiscount());
			/* 更新订单 */
			this.ordersDao.update(orders);
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersService(AOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		orders.setUpdateTime(new Date());
		int res = this.ordersDao.update(orders);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AOrders> findCondListOrdersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<AOrders> apiResponse = new ApiResponse<AOrders>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<AOrders> demoOrdersList = Collections.EMPTY_LIST;

		Map<String, List<AOrders>> dataList = new HashMap<String, List<AOrders>>();
		if(pageInfoUtil != null){
			Page<Object> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			demoOrdersList = this.ordersDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
		}else {
			demoOrdersList = this.ordersDao.findCondList(condMap);
		}
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersList.iterator(); iterator.hasNext();)
			{
				AOrders orders = (AOrders) iterator.next();
				
				extMap.clear();
				extMap.put("id", orders.getUsersId());
				ApiResponse<AUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
				AUsers users = cateApiResponse.getDataOneJava();
				orders.setUsers(users);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AOrders> findOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<AOrders> apiResponse = new ApiResponse<AOrders>();

		Map<String, AOrders> data = new HashMap<String, AOrders>();
		AOrders orders = this.ordersDao.findOne(condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", orders.getUsersId());
			ApiResponse<AUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
			AUsers users = cateApiResponse.getDataOneJava();
			orders.setUsers(users);
		}
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), orders);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(orders);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		ordersGoods.setCreateTime(new Date());
		ordersGoods.setUpdateTime(new Date());
		int res = this.ordersGoodsDao.save(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		ordersGoods.setUpdateTime(new Date());
		int res = this.ordersGoodsDao.update(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersGoodsDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AOrdersGoods> findCondListOrdersGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<AOrdersGoods> apiResponse = new ApiResponse<AOrdersGoods>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<AOrdersGoods> demoOrdersGoodsList = Collections.EMPTY_LIST;

		Map<String, List<AOrdersGoods>> dataList = new HashMap<String, List<AOrdersGoods>>();
		if(pageInfoUtil != null){
			Page<Object> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			demoOrdersGoodsList = this.ordersGoodsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
		}else {
			demoOrdersGoodsList = this.ordersGoodsDao.findCondList(condMap);
		}
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersGoodsList.iterator(); iterator.hasNext();)
			{
				AOrdersGoods ordersGoods = (AOrdersGoods) iterator.next();
				
				extMap.clear();
				extMap.put("id", ordersGoods.getOrdersId());
				ApiResponse<AOrders> ordersApiResponse = this.findOneOrdersService(extMap);
				AOrders orders = ordersApiResponse.getDataOneJava();
				ordersGoods.setOrders(orders);
				
				extMap.clear();
				extMap.put("id", ordersGoods.getGoodsId());
				ApiResponse<AGoods> goodsApiResponse = this.goodsDbService.findOneGoodsService(extMap);
				AGoods goods = goodsApiResponse.getDataOneJava();
				ordersGoods.setGoods(goods);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersGoodsList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersGoodsList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AOrdersGoods> findOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<AOrdersGoods> apiResponse = new ApiResponse<AOrdersGoods>();

		Map<String, AOrdersGoods> data = new HashMap<String, AOrdersGoods>();
		AOrdersGoods ordersGoods = this.ordersGoodsDao.findOne(condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", ordersGoods.getOrdersId());
			ApiResponse<AOrders> ordersApiResponse = this.findOneOrdersService(extMap);
			AOrders orders = ordersApiResponse.getDataOneJava();
			ordersGoods.setOrders(orders);
			
			extMap.clear();
			extMap.put("id", ordersGoods.getGoodsId());
			ApiResponse<AGoods> goodsApiResponse = this.goodsDbService.findOneGoodsService(extMap);
			AGoods goods = goodsApiResponse.getDataOneJava();
			ordersGoods.setGoods(goods);
		}
		data.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(ordersGoods);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
}
